package com.practice.car.cardataapp.service.impl;

import com.practice.car.cardataapp.entity.CarSaleMonth;
import com.practice.car.cardataapp.entity.frontEnd.Form;
import com.practice.car.cardataapp.entity.frontEnd.Legend;
import lombok.Data;
import lombok.Getter;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.function.Consumer;
@Data
/**
 * car-data-app project
 *
 * @author ZhangMingXing
 * @date 2020/9/6
 */
public class SeriesUtil<T> {
	private LegendSolver<T> legendSolver;
	private FormSolver<T> formSolver;
	private String type="line";
	private Legend legend;


	public List<Form> getSeries(List<T> list) {
		ArrayList<Form> forms = new ArrayList<>();
		Legend legend = getLegend(list);
		legend.getData().forEach(name -> {
			Form form = new Form();
			form.setData(new ArrayList<>());
			form.setType(type);
			form.setName(name);
			list.forEach(
					//提取 list 中 legend相同的数据作为一个 form
					e->{
						if (name.equals(legendSolver.solve(e))) {
							form.getData().add(formSolver.solve(e));
						}
					});
			forms.add(form);
		});
		return forms;
	}
	public Legend getLegend(List<T> list) {
		if (legend != null) {
			return legend;
		}
		legend = new Legend();
		LinkedHashSet<String> stringSet = new LinkedHashSet<>();
		list.forEach(
			// 提取 list 中作为 legend图标系列名字 的数据
				e-> stringSet.add(legendSolver.solve(e)));
		List<String> data = new ArrayList<>();
		stringSet.forEach(data::add);
		legend.setData(data);
		return legend;
	}

	interface LegendSolver<T> {
		String solve(T t);
	}
	interface FormSolver<T>{
		Object solve(T t);
	}
}
